package com.itboye.ihomebank;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.controller.LoginController;
import com.itboye.ihomebank.custom.RoundImageView;
import com.itboye.ihomebank.home.ActivitySwitchRoles;
import com.itboye.ihomebank.login.LoginActivity;
import com.itboye.ihomebank.me.ActivityFaBuHouse;
import com.itboye.ihomebank.me.ActivityGuanJia;
import com.itboye.ihomebank.me.ActivityInviteFriends;
import com.itboye.ihomebank.me.ActivityMyData;
import com.itboye.ihomebank.me.ActivitySetting;
import com.itboye.ihomebank.me.ActivityWoDeQiuZu;
import com.itboye.ihomebank.me.WodeFangyuanActivity;
import com.itboye.ihomebank.me.key.ActivityAdministrationKey;
import com.itboye.ihomebank.me.key.ActivityMyKey;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;

/**
 * 我的
 * 
 * @author
 * 
 */
public class MeFragment extends BaseFragment implements OnClickListener {
	TextView person_qiehuan;// 切换角色
	ImageView person_shezhi;// 设置
	RoundImageView roundImage;
	TextView name;
	ImageView queryPersonData;
	TextView phone_num;
	LinearLayout lin_mywallet;// 钱包
	LinearLayout lin_mycollection;// 收藏
	LinearLayout lin_myappointment;// 预约
	LinearLayout lin_mysign;// 签约
	RelativeLayout adddress_manage;// 确认房源
	TextView address_tv;// 房源地址
	RelativeLayout mylifemanager_rel;// 生活管家
	RelativeLayout myintelligent_lock_rel;// 智能锁
	RelativeLayout myassess_rel;// 评价
	RelativeLayout invitefriend_rel;// 邀请好友
	RelativeLayout help_rel;// 帮助中心
	LinearLayout head;// 头像（整块）
	TextView geren_tv01;
	String role = null;
	private String identity;

	@Override
	public int initView() {
		return R.layout.activity_person_centre;
	}

	@Override
	public void onMyActivityCreated() {
		role = SPUtils.get(getActivity(), null, SPContants.ROLE, "") + "";
		identity = SPUtils.get(getActivity(), null,
				SPContants.IdentityValidate, "") + "";
		if (identity != null) {
			if (identity.equals("1")) {
				geren_tv01.setText("经纪人");
			} else {
				geren_tv01.setText("个人房东");
			}
		} else {
			geren_tv01.setText("游客");
		}
		myintelligent_lock_rel.setOnClickListener(this);

	}

	@Override
	public void onResume() {
		super.onResume();
		read();
	}

	/**
	 * 加入数据
	 */
	private void read() {
		String nikeName = (String) SPUtils.get(getActivity(), null,
				SPContants.NICK, "");
		name.setText("" + nikeName);

		String mobile = (String) SPUtils.get(getActivity(), null,
				SPContants.MOBILE, "");

		if (!mobile.equals("")) {
			try {
				String hintphone = mobile.substring(0, 3) + "****"
						+ mobile.substring(7, 11);
				phone_num.setText(hintphone);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println(">>>>>>head"
				+ (String) SPUtils.get(getActivity(), null,
						SPContants.AVATAR_URL, ""));
		String resultData = (String) SPUtils.get(getActivity(), null,
				SPContants.AVATAR_URL, "");
		if (resultData != null) {
			if (resultData.startsWith("http")) {
				XImageLoader.load(resultData, roundImage);
			} else {
				if (resultData.equals("0")) {
					roundImage.setBackgroundResource(R.drawable.default_head);
				} else {
					XImageLoader.load(NetPublicConstant.IMAGE_URL + resultData,
							roundImage);
				}
			}
		} else {
			roundImage.setBackgroundResource(R.drawable.default_head);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		IntentFilter intentFilterAvart = new IntentFilter(
				ActivityMyData.AVARTACTION);// 当收到头像修改广播时修改头像
		getActivity().registerReceiver(avartBroadcastReceiver,
				intentFilterAvart);

		IntentFilter intentFilterExit = new IntentFilter(
				ActivitySetting.EXITCHANGE);// 当收到退出登录广播的时候执行
		getActivity()
				.registerReceiver(avartBroadcastReceiver, intentFilterExit);
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.person_qiehuan:// 切换角色
			intent = new Intent(getActivity(), ActivitySwitchRoles.class);
			startActivity(intent);
			break;
		case R.id.person_shezhi:// 设置
			intent = new Intent(getActivity(), ActivitySetting.class);
			startActivity(intent);
			break;
		case R.id.head:
			LoginController.onMyPersonalCenter(getActivity(), null);
			break;
		case R.id.lin_mywallet:// 钱包
			LoginController.onMyMoneyClick(getActivity(), null);
			break;
		case R.id.lin_mycollection:// 收藏
			LoginController.onMyCollectionClick(getActivity(), null);
			break;
		case R.id.lin_myappointment:// 预约
			LoginController.onMyAppointmentClick(getActivity(), null);
			break;
		case R.id.lin_mysign:// 签约
			LoginController.onMySignClick(getActivity(), null);
			break;
		case R.id.adddress_manage:// 确认房源
			intent = new Intent(getActivity(), WodeFangyuanActivity.class);
			startActivity(intent);
			break;
		case R.id.mylifemanager_rel:// 生活管家
			intent = new Intent(getActivity(), ActivityGuanJia.class);
			startActivity(intent);
			break;
		case R.id.myintelligent_lock_rel:// 智能锁
			intent = new Intent(getActivity(), ActivityMyKey.class);
			startActivity(intent);
			break;
		case R.id.myassess_rel:// 我的租房
			intent = new Intent(getActivity(), ActivityWoDeQiuZu.class);
			startActivity(intent);
			break;
		case R.id.invitefriend_rel:// 邀请好友
			intent = new Intent(getActivity(), ActivityInviteFriends.class);
			startActivity(intent);
			break;
		case R.id.help_rel:// 帮助中心（跳转到登录 测试）
			intent = new Intent(getActivity(), LoginActivity.class);
			startActivity(intent);
			break;

		case R.id.geren_tv01:
			// geren_tv01.setAlpha(0.8f);
			// geren_tv02.setAlpha(0.4f);
			// geren_tv03.setAlpha(0.4f);
			break;
		case R.id.queryPersonData:
			intent = new Intent(getActivity(), ActivityMyData.class);
			startActivity(intent);
		default:
			break;
		}
	}

	/**
	 * 广播 当收到头像更改 更换头像
	 */
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			read();
		};
	};

}
